package main;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import algorithms.Combination;
import redis.clients.jedis.Jedis;

/**
 * Servlet implementation class Arrangement
 */
@WebServlet("/Arrange")
public class Arrange extends HttpServlet {
	int beginN = 0;	
	int endN = 0;
	long ordinalTime;
	long dictTime;
	long transTime;
	List<String> list;
	Combination combination;
	Jedis jedis;
	
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Arrange() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    
    public void init() throws ServletException {
    	combination = new Combination();
    	jedis = new Jedis("localhost");
    	jedis.set("testKey", "testValue");
		
	}
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//bean
		System.out.println("jjjjjj"+jedis.ping());
		try {
			beginN = Integer.parseInt(request.getParameter("beginN"));
			endN = Integer.parseInt(request.getParameter("endN"));	
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println(beginN);
		PrintWriter out = response.getWriter();
		
		if (beginN != 0 && beginN <= endN) {
			for (int n = beginN; n <= endN; n++) {
				combination.setN(n);
				long ot = combination.ordinalTime();
				long oop = combination.ordinalOp;
				
				long dt = combination.dictTime();
				long dop = combination.dictOp;
				
				long tt = combination.transTime();
				long top = combination.transOp;
				
				jedis.lpush("ordinalTime:" + n, Long.toString(ot));
				jedis.lpush("dictTime:" + n, Long.toString(dt));
				jedis.lpush("transTime:" + n, Long.toString(tt));
				
				jedis.set("ordinalOp:" + n, Long.toString(oop));
				jedis.set("dictOp:" + n, Long.toString(dop));
				jedis.set("transOp:" + n, Long.toString(top));
			}
			
			out.println("{");
			for (int n = beginN; n <= endN; n++) {
				List<String> ordinalList = jedis.lrange("ordinalTime:" + n, 0, jedis.llen("ordinalTime:"+n));
				ordinalTime = Combination.avrTime(ordinalList);
				String ordinalOp = jedis.get("ordinalOp:" + n);
				out.print("\"ordinalTime" + n + "\":" + ordinalTime  + ",");
				out.print("\"ordinalOp" + n + "\":" + Long.parseLong(ordinalOp)  + ",");
				
				List<String> dictList = jedis.lrange("dictTime:" + n, 0, jedis.llen("dictTime:"+n));
				dictTime = Combination.avrTime(dictList);
				String dictOp = jedis.get("dictOp:" + n);
				out.print("\"dictTime" + n + "\":" + dictTime + ",");
				out.print("\"dictOp" + n + "\":" + Long.parseLong(dictOp) + ",");
				
				List<String> transList = jedis.lrange("transTime:" + n, 0, jedis.llen("transTime:"+n));
				transTime = Combination.avrTime(transList);
				String transOp = jedis.get("transOp:" + n);
				if (n != endN) {
					out.println("\"transTime" + n + "\":" + transTime + ",");
					out.println("\"transOp" + n + "\":" + Long.parseLong(transOp) + ",");
				}else{
					out.println("\"transTime" + n + "\":" + transTime + ",");
					out.println("\"transOp" + n + "\":" + Long.parseLong(transOp));
				}
			}
			out.println("}");
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	public void destroy() {
		jedis.del("ordinalTime:0","dictTime:0","transTime:0");
	}

}
